473,423 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,423 software developers and data experts.

How to use a radio button with php

348 100+
Hi everyone,

Can somebody please tell me or maybe show me an example of how to use multiple radio buttons in php?

I have a form with 1 textbox and 4 radio buttons. What I want to do is have a user, type in a search string and then check a radio button that will cooraspond to a field in a database. The problem I am having is that the input name must be the same for all of the radio buttons to ensure that only one button is checked. All four buttons must have the same name to be in the same group within the html.

If I make all of the names the same, then how do I differentiate between one POST value and another? I was thinking that maybe from the html button's value but I can't get php to see it.

Can someone please provide a sample or the direction I need to go in?

Thanks,

Frank
Sep 22 '07 #1
12 54391
bergy
89
How you tell is in the value of the variable (also set in the value attribute of the input tag). Look at the radio buttons below:
[html]
<input type="radio" name="radio" value="radio" />
<input type="radio" name="radio" value="radio2" />
<input type="radio" name="radio" value="radio3" />
[/html]
[php]echo $_POST['radio'];[/php]
If the first radio button was checked the code above would output "radio", if the second was checked the very same code would output "radio2" and if the third was checked the very same code would output "radio3".

So basically, one variable 3 different possible values for that variable depending what which radio button you've checked.
Sep 22 '07 #2
bergy
89
I assume you could just set the value of the radio buttons to correspond with the column name of your table so...

[html]
<input type="text" name="textBox" />
<input type="radio" name="updateField" value="name" />
<input type="radio" name="updateField" value="address" />
<input type="radio" name="updateField" value="phoneNumber" />
[/html]

[php]
$column = $_POST['updateField'];
$value = $_POST['textBox'];
mysql_query("update myTable set ".$column." = `".$value."`;");
[/php]
Sep 22 '07 #3
fjm
348 100+
How you tell is in the value of the variable (also set in the value attribute of the input tag). Look at the radio buttons below:
[html]
<input type="radio" name="radio" value="radio" />
<input type="radio" name="radio" value="radio2" />
<input type="radio" name="radio" value="radio3" />
[/html]
[php]echo $_POST['radio'];[/php]
If the first radio button was checked the code above would output "radio", if the second was checked the very same code would output "radio2" and if the third was checked the very same code would output "radio3".

So basically, one variable 3 different possible values for that variable depending what which radio button you've checked.
Bergy,

Thank you very much for the explanation. I am going to try and see if I can get it working with what you gave me.

I have exactly what you have as far as all of the name values being the same and a different value assigned to each button but I can't get it to work. I'm sure this problem has much to do with my lack of php experience.


Thanks,

Frank
Sep 22 '07 #4
fjm
348 100+
I assume you could just set the value of the radio buttons to correspond with the column name of your table so...

[html]
<input type="text" name="textBox" />
<input type="radio" name="updateField" value="name" />
<input type="radio" name="updateField" value="address" />
<input type="radio" name="updateField" value="phoneNumber" />
[/html]

[php]
$column = $_POST['updateField'];
$value = $_POST['textBox'];
mysql_query("update myTable set ".$column." = `".$value."`;");
[/php]
Hey Bergy,

That is exactly what I have done. Each value coorasponds to a field in my db.

Let me ask you this Bergy, if you wanted to SELECT a different column that would cooraspond to a different radio button in your example, how would you write your sql. Maybe that is where I am getting hung up.

I have 4 buttons relating to a vehicle search. make, model, vin and license_Plate

Each button has its own value such as
Expand|Select|Wrap|Line Numbers
  1. <input type="radio" name="vehicle" value="plate" />License Plate
the way I am doing it now is writing 4 different sql statements such as

SELECT * FROM vehicle where license_Plate = $_POST[vehicle]
SELECT * FROM vehicle where vin = $_POST[vehicle]

My issue is that I think I need to use an if conditional to find the correct sql code. I can only do that with its value. I'm sorry if I am not clear.

Frank
Sep 22 '07 #5
bergy
89
I just tested it to make sure and it does in fact work for me. When trying to figure out minor stuff like this, I like to just do a print_r(_POST); which will print everything posted from the form. Copy and paste the following code into a PHP file and you should get someting like [updateField] => name if you check the first radio button.

[php]
<form id="form1" name="form1" method="post" action="">
<input type="text" name="textBox" />
<input type="radio" name="updateField" value="name" />
<input type="radio" name="updateField" value="address" />
<input type="radio" name="updateField" value="phoneNumber" />
<input type="submit" name="button" id="button" value="Submit" />
</form>
<pre>
<?php
print_r($_POST);
?>
</pre>
[/php]
Sep 22 '07 #6
fjm
348 100+
I just tested it to make sure and it does in fact work for me. When trying to figure out minor stuff like this, I like to just do a print_r(_POST); which will print everything posted from the form. Copy and paste the following code into a PHP file and you should get someting like [updateField] => name if you check the first radio button.

[php]
<form id="form1" name="form1" method="post" action="">
<input type="text" name="textBox" />
<input type="radio" name="updateField" value="name" />
<input type="radio" name="updateField" value="address" />
<input type="radio" name="updateField" value="phoneNumber" />
<input type="submit" name="button" id="button" value="Submit" />
</form>
<pre>
<?php
print_r($_POST);
?>
</pre>
[/php]
Thanks for sticking with me Bergy. I have already used print_r($POST);

My output is the key which is vehicle and the value which is whatever button was pressed which is correct.

( [search] => [vehicle] => plate
Sep 22 '07 #7
bergy
89
The value in <input type="radio" value="something"> should be the exact name of your column. So if your column's name is license_Plate you need to make it "license_Plate" not "plate" - this way you won't need any if statements.

I think your SQL is incorrect it shouldn't be:
SELECT * FROM vehicle where license_Plate = $_POST[vehicle]

But rather:
SELECT * FROM vehicle where $_POST['vehicle'] = 'searchTerm'

(edit - clarification) This way, if the radio button for license plate is checked, $_POST['vehicle'] = license_Plate and that is the column used in your SQL query. Here is the PHP i would use:

[php]
mysql_query("SELECT * from vehicle where ".$_POST['vehicle']." like 'searchTerm'");
[/php]
I assume searchTerm would be replaced with the value from the textbox? So, $_POST['textBoxName']?
Sep 22 '07 #8
fjm
348 100+
[php] if(isset($_POST['vehicle'])){
if($_POST['plate']){
echo "plate";
}elseif($_POST['vin']){
echo "vin";

}else{
echo "none";
}
}[/php]

Here is the php
Sep 22 '07 #9
fjm
348 100+
The value in <input type="radio" value="something"> should be the exact name of your column. So if your column's name is license_Plate you need to make it "license_Plate" not "plate" - this way you won't need any if statements.
I'm a bit confused Bergy. Sorry.. Ok, I can change my radio button values to exactly match the column names in the db. thats cool. But I am not following you on the sql. I have never seen sql written the way you are showing me. :)

SELECT * FROM vehicle where $_POST['vehicle'] = 'searchTerm'
the way I am reading this, the post global will output "vehicle" and the rest will follow.

vehicle = search_term??

I was looking to use the post global to match the column name in the db. Of course, if I can use the value, thats great too but I'm sorry, I don't quite understand how to do that with your example.

Ok, here is what I have in my html. I have 1 textbox and 4 radio buttons. I want to use the html to complete the sql statements.

So... If the user enters 2ERQ267 in the textbox for a license plate and then checks the license plate button, *I was thinking* to have an if conditional check to see if the license_Plate button was checked. If so, execute that sql. Else if html button VIN was checked, execute the VIN sql.

edit:

I have 4 different columns that I want to search on and 1 textbox to input the information into.
Sep 22 '07 #10
fjm
348 100+
Bergy,


This works.

[php] if(isset($_POST['vehicle'])){
if($_POST['vehicle'] == "plate"){
echo "plate"; [/php]
Is that what you were saying I should do?
Sep 22 '07 #11
bergy
89
Fjm,

You will be using the _POST['vehicle'] as the column name inside of SQL. PHP evaluates the statement before it goes to MySQL...

SELECT * FROM table_name WHERE column_name = value

The above statement is what you're trying to accomplish? Based on what you've said, your table_name is "vehicle", you want to use radio buttons to drive the column_name, and I'm assuming that value is coming from a text box on the form?

If my above assumptions are correct, and you set your radio buttons values to your column names, your SQL statement in PHP would look like this:
[php]"SELECT * FROM vehicle WHERE ".$_POST['vehicle']." = 'something';"[/php]
The 'something' would be replaced by the POST variable for your textbox. And based on the above post of yours, I'm assuming all of your radio buttons are named "vehicle" (which can be a little confusing I guess since that is the same name of the table).

If you set the value of your raido buttons to the names of your 3 columns, all of the following could be the result of your MySQL statement (after PHP parses it, BEFORE it goes to MySQL):

SELECT * FROM vehicle WHERE license_Plate = value
SELECT * FROM vehicle WHERE vin = value
SELECT * FROM vehicle WHERE make = value
SELECT * FROM vehicle WHERE model = value
etc...

I hope this makes it clear - or maybe this isn't what you're trying to do - I'm not sure.
Sep 22 '07 #12
fjm
348 100+
Fjm,

You will be using the _POST['vehicle'] as the column name inside of SQL. PHP evaluates the statement before it goes to MySQL...

SELECT * FROM table_name WHERE column_name = value

The above statement is what you're trying to accomplish? Based on what you've said, your table_name is "vehicle", you want to use radio buttons to drive the column_name, and I'm assuming that value is coming from a text box on the form?

If my above assumptions are correct, and you set your radio buttons values to your column names, your SQL statement in PHP would look like this:
[php]"SELECT * FROM vehicle WHERE ".$_POST['vehicle']." = 'something';"[/php]
The 'something' would be replaced by the POST variable for your textbox. And based on the above post of yours, I'm assuming all of your radio buttons are named "vehicle" (which can be a little confusing I guess since that is the same name of the table).

If you set the value of your raido buttons to the names of your 3 columns, all of the following could be the result of your MySQL statement (after PHP parses it, BEFORE it goes to MySQL):

SELECT * FROM vehicle WHERE license_Plate = value
SELECT * FROM vehicle WHERE vin = value
SELECT * FROM vehicle WHERE make = value
SELECT * FROM vehicle WHERE model = value
etc...

I hope this makes it clear - or maybe this isn't what you're trying to do - I'm not sure.
Bergy,

Thank you again for clairifying your example. You are right on the money with what I needed to acomplish.

With your examples last night, I was able to get it to work.

What was really confusing me was using the word "vehicle" in the html because it was also the same name for the table.

I studied your example last night and played with it and was able to get it working. Once I got it, I looked at it a bit closer and I now can see exactly how it works.

Thanks for helping me Bergy!

Frank
Sep 22 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: John Davis | last post by:
I created a ASP.NET Web Form using VB.NET with a text box, 2 radio buttons. When the user click the first radio button, the text will change to uppercase. If the user clicks the other radio button,...
0
by: vinay | last post by:
why are u not using the radiobutton list??? vinay >-----Original Message----- >I created a simple ASP.NET application with a text field, and 2 radio >buttons (uppercase and lowercase...
3
by: Amelyan | last post by:
When we want radio button to belong to a group name we say, radio1.GroupName="GroupA". In this case, radio1 will be unselected if another radio button is selected in "GroupA". Is there a way...
8
by: stefano | last post by:
HI, I have aproblem with XHTML radio button. <form name=" form"> <input name="radio" type="radio" onclick="return false" /> <input name="radio" type="radio" onclick="return false" /> <input...
7
by: nathaniel.k.lee | last post by:
Is it not possible, in IE, to dynamically click a radio button? I'm grabbing some values from a database and using them to populate radio buttons on a page. I have alternate code for Firefox...
9
by: IchBin | last post by:
I can not see what the problem is with this script. I am just trying to set a radio button by calling setCheckedValue('abbr_letter', 'V'). Sorry I am new to javascript. <html> <head> <script...
1
by: IchBin | last post by:
I am trying to set the state of a radio button. I do not see what I am doing wrong. Sorry, I am new at this.. I need another set of eyes to look at this snip of code. I am trying to set the radio...
10
by: IchBin | last post by:
I am trying to set the state of a radio button. I do not see what I am doing wrong. Sorry, I am new at this.. I need another set of eyes to look at this snip of code. I am trying to set the radio...
0
by: jehugaleahsa | last post by:
Hello: I have radio buttons bound to boolean properties in a business object. private void bindRadioButton(RadioButton button, string propertyName) { Binding binding =...
8
by: photoboy | last post by:
I have racked by brain long enough on this, so now I need the help of someone who knows what they are doing. Here is what I am trying to achieve: First, I have two radio buttons (both unchecked)...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.